$(document).ready(function(){
	getLinksFromServer();
});

function getLinksFromServer(){
	var leftAccordion = "#div_left_accordion";
	var url = 'manage/getFunctionTreeJson.do?id=F001&timeflag='+Math.random();
	$(leftAccordion).mask({msg:'正在加载链接。。。'});
	$.getJSON(url,function(data){
		if(null != data.code && data.code == 'error'){
			if(null != data.msg){
				alert(data.msg);
			}else{
				alert('操作出现异常！');
			}
			$(leftAccordion).ummask();
			return ;
		}
		
		addAccordionPanel(data);
		$(leftAccordion).unmask();
	});
	try{
		updateLinkBehavior();
	}catch(ex){
		alert('出现异常！'+ex);
	}
}
/**
 * 添加导航区域的面板
 * @param {} data
 */
function addAccordionPanel(data){
	var leftAccordion = "#div_left_accordion";
	var firstPanel = "";
	$(data.subTree).each(function(i,item){
		var setting = {
			id		:item.id,
			title	:item.name,
			content :getAccordionPanelLink(item)
		};
		if(firstPanel == ""){
			firstPanel = item.name;
		}
		$(leftAccordion).accordion('add',setting);	
	});
	
	$(leftAccordion).accordion('select',firstPanel);	
}
/**
 * 添加面板内的链接
 * @param {} data
 */
function getAccordionPanelLink(data){
	var ul = '<ul class="linkList">\n';
	if(null!=data && null != data.subTree && data.subTree.length>0){
		$(data.subTree).each(function(i,item){
			ul = ul + '<li><a href="'+item.url+'" title="'+item.name+'">'+item.name+'</a></li>\n';
		});
	}
	ul=ul+'</ul>';
	return ul;
}